package ru.mipt.simt.ui;

import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JPanel;
import ru.mipt.simt.Road;
import ru.mipt.simt.Car;

public class RoadPanel extends JPanel
{
	private Road road;
	private double s0;
	private double length;
	private double sScale = 1.;

	public RoadPanel(Road road, double s0, double length)
	{
		this.road = road;
		this.length = length;
		this.s0 = s0;
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		sScale = getWidth() / length;

		ArrayList<Car> cars = road.getCars();
		for (int i = 0; i < cars.size(); i++)
		{
			Car car = cars.get(i);
			if (car.s > s0 || car.s < s0 + length)
			{
				paintCar(car, g);
			} else if (car.s > s0 + length)
				break;
		}
	}

	private void paintCar(Car car, Graphics g)
	{
		g.drawRect((int) ((car.s - s0) * sScale), 50, 10, 4);
	}

}
